package xyz.zcxsythenew.app

import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.fragment.app.Fragment

/**
 * A simple [Fragment] subclass as the default destination in the navigation.
 */
class FirstFragment : Fragment() {

    override fun onCreateView(
            inflater: LayoutInflater, container: ViewGroup?,
            savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_first, container, false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val act = activity as MainActivity
        if (act.login) {
            val textView = view.findViewById<TextView>(R.id.textview_first)
            textView.text = act.tokenString
            val button = view.findViewById<Button>(R.id.button_first)
            button.text = getString(R.string.refresh)
        }

        view.findViewById<Button>(R.id.button_first).setOnClickListener {
            /* val action = FirstFragmentDirections.actionFirstFragmentToSecondFragment("From FirstFragment")
            findNavController().navigate(action) */
            if (act.login) {
                val button = it.findViewById<Button>(R.id.button_first)
                button.isEnabled = false
                act.refresh()
            } else {
                val i = Intent()
                i.action = "android.intent.action.VIEW"
                val uri = Uri.parse("https://cas.zcxsythenew.xyz/oauth/authorize?response_type=code&client_id=1")
                i.data = uri
                startActivity(i)
            }
        }
    }
}